﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Shl01.Models;

namespace Shl01.Dal
{
    public class BLL
    {
        public void Ma(Conn con, string DatabaseName, string Path, List<string> TableName)
        {
            string ConnStr = "";
            if (con.Type == 0)
            {
                ConnStr = string.Format("serVer = {0}; database = {3};uid = {1};pwd = {2}", con.Fuwu, con.Id, con.Pwd, DatabaseName);
            }
            else
            {
                ConnStr = string.Format("Data Source={0};Initial Catalog={1};Integrated Security=True", con.Fuwu, DatabaseName);
            }
            using (SqlConnection conn = new SqlConnection(ConnStr))
            {
                conn.Open();
                foreach (string str in TableName)
                {
                    using (SqlCommand cmd = new SqlCommand("exec sp_columns [" + str + "]", conn))
                    {
                        Dictionary<string, string> dic = new Dictionary<string, string>();
                        SqlDataReader reader = cmd.ExecuteReader();
                        while (reader.Read())
                        {
                            string type = reader["TYPE_NAME"].ToString();
                            switch (type)
                            {
                                case "char":
                                    type = "string";
                                    break;
                                case "nchar":
                                    type = "string";
                                    break;
                                case "varchar":
                                    type = "string";
                                    break;
                                case "nvarchar":
                                    type = "string";
                                    break;
                                case "int":
                                    type = "int";
                                    break;
                                case "smallint":
                                    type = "int";
                                    break;
                                case "datetime":
                                    type = "DateTime";
                                    break;
                                case "float":
                                    type = "float";
                                    break;
                                case "smalldatetime":
                                    type = "DateTime";
                                    break;
                                case "bit":
                                    type = "bool";
                                    break;
                                case "decimal":
                                    type = "double";
                                    break;
                                case "money":
                                    type = "double";
                                    break;
                                case "int identity":
                                    type = "int";
                                    break;
                            }
                            string Name = reader["COLUMN_NAME"].ToString();
                            dic.Add(Name, type);
                        }
                        reader.Close();
                        SqlCommand cmds = new SqlCommand("exec sp_helpconstraint [" + str + "]", conn);
                        SqlDataAdapter adapter = new SqlDataAdapter();
                        adapter.SelectCommand = cmds;
                        DataSet ds = new DataSet();
                        adapter.Fill(ds);
                        string PrimaryKey = "";
                        foreach (DataRow dr in ds.Tables[1].Rows)
                        {
                            string zhu = dr["constraint_type"].ToString();
                            if (zhu == "PRIMARY KEY (clustered)")
                            {
                                PrimaryKey = dr["constraint_keys"].ToString();
                            }
                        }
                        BLLModel service = new BLLModel();
                        service.TypeName = str;
                        service.PrimaryName = PrimaryKey;
                        service.DBName = DatabaseName;
                        using (FileStream fs = new FileStream(string.Format("{0}\\{1}Manager.cs", Path, str), FileMode.Create))
                        {
                            using (StreamWriter sw = new StreamWriter(fs))
                            {
                                sw.WriteLine(service.GetStr());
                            }
                        }
                    }
                }
            }
        }
    }
}
